//
//  UIScrollView+INSRefresh.m
//  XWKit
//
//  Created by xiaowuxiaowu on 16/3/29.
//  Copyright © 2016年 xiaowuxiaowu. All rights reserved.
//
#import "UIScrollView+INSRefresh.h"
#import "UIView+MBProgressHUD.h"
#import "XWDefaultInfiniteIndicator.h"
static CGFloat const ins_MaxHeight = 65.0f; //显示高度



@implementation UIScrollView (INSRefresh)



/* 添加上拉加载
 * handlerBlock : UIScrollView回调
 */
-(void)xw_addPullToRefreshWithHandler:(void(^)(UIScrollView *scrollView))handlerBlock{
    [self ins_addPullToRefreshWithHeight:ins_MaxHeight handler:handlerBlock];
    
    
    
    UIView <INSPullToRefreshBackgroundViewDelegate> *pullToRefresh = [self pullToRefreshViewFromCurrentStyle];
    
    self.ins_pullToRefreshBackgroundView.delegate = pullToRefresh;
    
    [self.ins_pullToRefreshBackgroundView addSubview:pullToRefresh];
}

/**
 结束
 */
-(void)xw_endPullToRefresh{
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UITableView *table = (UITableView *)self;
        [table reloadData];
        
        if([NSThread mainThread]){
            [self ins_endPullToRefresh];
            self.ins_infiniteScrollBackgroundView.enabled = [self getNumberOfRows]%ins_MaxNumber == 0;
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self ins_endPullToRefresh];
                self.ins_infiniteScrollBackgroundView.enabled = [self getNumberOfRows]%ins_MaxNumber == 0;
            });
        }
        
    });
    
    
}



-(void)xw_addInfinityScrollWithHandler:(void(^)(UIScrollView *scrollView))handlerBlock{
    
    [self ins_addInfinityScrollWithHeight:ins_MaxHeight handler:handlerBlock];
    UIView <INSAnimatable> *infinityIndicator = [self infinityIndicatorViewFromCurrentStyle];
    [self.ins_infiniteScrollBackgroundView addSubview:infinityIndicator];
    [infinityIndicator startAnimating];
    self.ins_infiniteScrollBackgroundView.preserveContentInset = YES;
    
    
}
-(void)xw_endInfinityRefreshAnimation:(BOOL)animation{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self endInfinityRefreshAnimation:animation];
        });
        
    });
    
    
}

-(void)endInfinityRefreshAnimation:(BOOL)animation{
    
    UITableView *table = (UITableView *)self;
    NSInteger x = [self getNumberOfRows];
    
    
    //    [table beginUpdates];
    //    numberOfRows += ins_MaxNumber;
    //    NSMutableArray* newIndexPaths = [NSMutableArray new];
    //
    //    for(NSInteger i = numberOfRows - ins_MaxNumber; i < numberOfRows; i++) {
    //        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    //        [newIndexPaths addObject:indexPath];
    //    }
    //
    //    [table insertRowsAtIndexPaths:newIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    //    [table endUpdates];
    
    [table reloadData];
    [self ins_endInfinityScrollWithStoppingContentOffset:YES]; //结束刷新
    self.ins_infiniteScrollBackgroundView.enabled = x == 0 ? NO: x%ins_MaxNumber == 0; //隐藏上提
}
-(NSInteger)getNumberOfRows{
    
    UITableView *table = (UITableView *)self;
    
    NSInteger numberOfRows = 0;
    if([self isKindOfClass:[UICollectionView class]]){
        UICollectionView *collection = (UICollectionView *)self;
        numberOfRows = [collection numberOfItemsInSection:0];
        if([collection numberOfSections]>1){  //如果是多numberOfSections 那么直接刷新
            for(NSInteger row = 1; row < [collection numberOfSections];row++){
                numberOfRows += [collection numberOfItemsInSection:row];
            }
            
        }
        
    }else{
        numberOfRows = [table numberOfRowsInSection:0];
        if([table numberOfSections]>1){  //如果是多numberOfSections 那么直接刷新
            for(NSInteger row = 1; row < [table numberOfSections];row++){
                numberOfRows += [table numberOfRowsInSection:row];
            }
            
        }
    }
    
    return numberOfRows;
    
}

-(void)xw_preserveContentInset{
    self.ins_pullToRefreshBackgroundView.preserveContentInset = YES;
}

/*
 * 设置参数
 */
//- (UIView <INSAnimatable> *)infinityIndicatorViewFromCurrentStyle {
//
//    CGRect defaultFrame = CGRectMake(0, 0, 24, 24);
//    return [[INSDefaultInfiniteIndicator alloc] initWithFrame:defaultFrame];
//
//}

- (UIView <INSPullToRefreshBackgroundViewDelegate> *)pullToRefreshViewFromCurrentStyle {
    
    
    CGRect defaultFrame = CGRectMake(0, 0, 32, 32);
    return [[XWDefaultInfiniteIndicator alloc] initWithFrame:defaultFrame];
    //
    //    CGRect defaultFrame = CGRectMake(0, 0, 24, 24);
    //    return [[INSDefaultPullToRefresh alloc] initWithFrame:defaultFrame backImage:[UIImage imageNamed:@"circleLight"] frontImage:[UIImage imageNamed:@"circleDark"]];
    
}

- (UIView <INSAnimatable> *)infinityIndicatorViewFromCurrentStyle {
    
    CGRect defaultFrame = CGRectMake(0, 0, 24, 24);
    return [[XWCircleInfiniteIndicator alloc] initWithFrame:defaultFrame];
    
    
}
@end

